/*    

    bobbietalk - a visual programming environment for toy robotics

    Copyright (C) 2013  Roberto Tiella

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/
package bobbietalk.semantics.compiler;

import bobbietalk.gui.ProgrammingBlock;
import bobbietalk.gui.RobotConfiguration;
import bobbietalk.gui.blocks.BeginBlock;
import bobbietalk.gui.blocks.EndBlock;
import bobbietalk.gui.blocks.parametrized.ParametrizedBlock;
import java.io.StringWriter;
import java.util.List;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;

/**
 *
 * @author roberto
 */
public class BshCompiler extends AbstractCompiler {

    private CompilationContext program;

    public BshCompiler(String tmpDir) { // TODO not used
        super(tmpDir);
        program = new CompilationContext();
    }

    @Override
    protected boolean generateCode(BlockGraph g, StringBuilder sb, List<String> messages, boolean isSubroutine) {

        assert !isSubroutine;
        
        ProgrammingBlock b = g.getBeginBlock();

        generateHeader(sb);

        int vc = 0;

        while (!(b instanceof EndBlock)) {

            b = b.getNextBlock();

            if (!compileLegacyBlocks(b, sb)) {

                if (!compileGenericBlocks(b, sb)) {

                    messages.add("block cannot be encoded: " + b);
                    return false;
                }
            }
        }

        return true;
    }

    protected boolean compileLegacyBlocks(ProgrammingBlock b, StringBuilder sb) {

        if (b instanceof BeginBlock) {
        } else if (b instanceof EndBlock) {
        } else {
            return false;
        }

        return true;
    }

    @Override
    protected boolean compileWithExternalTool(String ppath, List<String> messages) {


        throw new UnsupportedOperationException("should never been called");

    }

    private boolean compileGenericBlocks(ProgrammingBlock b, StringBuilder sb) {

        if (b instanceof ParametrizedBlock) {

            ParametrizedBlock pb = (ParametrizedBlock) b;

            String template = pb.getCodeTemplate("bsh");


            VelocityContext context = new VelocityContext();

            context.put("block", pb);
            context.put("robotconf", getRobotConfiguration());
            context.put("program", program);

            StringWriter sw = new StringWriter();

            Velocity.evaluate(context, sw, "compile-" + b.getName(), template);

            sb.append(sw.toString());
            sb.append('\n');

            return true;

        } else {

            return false;
        }
    }

    @Override
    public String getExension() {

        return "bsh";

    }

    private void appendRobotConfiguration(StringBuilder sb) {

        RobotConfiguration rc = getRobotConfiguration();

//        sb.append(String.format(
//                "SetSensorTouch(IN_%1$s);\n"+
//                "ResetSensor(IN_%1$s);\n",rc.getTouch1SensorPort()));
//        
//        sb.append(String.format(
//                "SetSensorTouch(IN_%1$s);\n"+
//                "ResetSensor(IN_%1$s);\n",rc.getTouch2SensorPort()));
//        
//        sb.append(String.format(
//                "SetSensorColorFull(IN_%1$s);\n"+
//                "ResetSensor(IN_%1$s);\n",rc.getColorSensorPort()));
//        
//        sb.append(String.format(
//                "SetSensorUltrasonic(IN_%1$s);\n"+
//                "ResetSensor(IN_%1$s);\n",rc.getDistanceSensorPort()));


    }

    private void generateHeader(StringBuilder sb) {
//        sb.append("task main() {").append('\n');

        appendRobotConfiguration(sb);
    }
}
